<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="5">
              <a-form-item label="部门" prop="type">
                <a-tree-select
                  v-model="queryParam.deptId"
                  style="width: 100%"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="deptOptions"
                  placeholder="请选择"
                  :replaceFields="{
                    children: 'children',
                    title: 'menuName',
                    key: 'id',
                    value: 'id',
                  }"
                  @change="onMenuTreeChange"
                  allow-clear
                >
                </a-tree-select>
              </a-form-item>
            </a-col>
            <a-col :span="5">
              <a-form-item label="员工">
                <a-select
                  placeholder="请选择员工"
                  v-model="queryParam.userId"
                  :getPopupContainer="
                    (triggerNode) => {
                      return triggerNode.parentNode || document.body;
                    }
                  "
                  style="width: 100%"
                  allow-clear
                >
                  <a-select-option
                    v-for="d in userNamesList"
                    :key="d.name"
                    :value="d.id"
                    >{{ d.name }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="5">
              <a-form-item label="统计时间">
                <a-month-picker
                  v-model="queryParam.createTime"
                  placeholder="选择月份"
                  style="width: 100%"
                  valueFormat="YYYY-MM-DD"
                />
              </a-form-item>
            </a-col>

            <a-col>
              <span
                class="table-page-search-submitButtons"
                style="float: right"
              >
                <a-button type="primary" @click="handleQuery"
                  ><a-icon type="search" />查询</a-button
                >
                <a-button style="margin-left: 8px" @click="resetQuery"
                  ><a-icon type="redo" />重置</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>

    <a-card :bordered="false" class="table-card" style="overflow-y: auto">
      <!-- 增加 -->
      <biz-cap-bill-add-form
        v-if="showAddModal"
        ref="bizCapBillAddForm"
        :typeOptions="typeOptions"
        :statusOptions="statusOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-cap-bill-edit-form
        v-if="showEditModal"
        ref="bizCapBillEditForm"
        :typeOptions="typeOptions"
        :statusOptions="statusOptions"
        @ok="getList"
        @close="showEditModal = false"
      />

      <advance-table
        title="资方结算"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) =>
            `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange,
        }"
        tableKey="base-statistics-yuangonggongzi-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="bizCapBillList"
        :loading="loading"
        :format-conditions="true"
        :scroll="{ x: '100%' }"
      >
        <div class="table-operations" slot="button">
          <a-button
            type=""
            @click="handleExport"
            v-hasPermi="['biz:bizCapBill:export']"
          >
            <a-icon type="download" />导出
          </a-button>
        </div>
        <span slot="type" slot-scope="{ record }">
          {{ typeFormat(record) }}
        </span>

        <!-- <span slot="status" slot-scope="{ record }">
          {{ statusFormat(record) }}
        </span> -->
        <span slot="orgType" slot-scope="{ record }">
          {{ orgTypeFormat(record) }}
        </span>

        <span slot="status" slot-scope="{ record }">
          <a-tag color="orange" v-if="record.status == 0">
            {{ statusFormat(record) }}
          </a-tag>
          <a-tag color="green" v-if="record.status == 1">
            {{ statusFormat(record) }}
          </a-tag>
        </span>
        <span slot="numStr" slot-scope="{ text }">
          ¥ {{ formatNumber(text) }}
        </span>
        <span slot="amount" slot-scope="{ text }">
          ¥ {{ formatNumber(text) }}
        </span>

        <span slot="rowindex" slot-scope="{ index }">
          {{ index + 1 }}
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import { exportOrgTypeStatistic } from "@/api/biz/bizCapBill";
import { listUser } from "@/api/system/user";
import { listDeptTree } from "@/api/system/dept";
import { yuangonggongzitongji } from "@/api/biz/bizCustomer";
import { listBizProductInfo } from "@/api/biz/bizProductInfo";
import AdvanceTable from "@/components/pt/table/AdvanceTable";
import BizCapBillAddForm from "@/views/biz/capbill/modules/BizCapBillAddForm";
import BizCapBillEditForm from "@/views/biz/capbill/modules/BizCapBillEditForm";
export default {
  name: "BizCapBill",
  components: {
    AdvanceTable,
    BizCapBillAddForm,
    BizCapBillEditForm,
  },
  data() {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 资方结算表格数据
      bizCapBillList: [],
      orgTypeOptions: [],
      // 结算类型字典
      typeOptions: [],
      // 结算状态字典
      statusOptions: [],
      dateRange: [],
      dateCheckRange: [],
      productList: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 50,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        jinjianId: undefined,

        cusName: undefined,

        beginQdTime: undefined,

        status: undefined,
      },
      hejidata: [],
      deptOptions: [],
      userNamesList: [],
      columns: [
        {
          title: "序号",
          dataIndex: "rowindex",
          ellipsis: true,
          align: "center",
          width: "45px",
          scopedSlots: { customRender: "rowindex" },
        },
        {
          title: "员工姓名",
          dataIndex: "userName",
          align: "center",
          width: "120px",
        },
        {
          title: "工资类型",
          dataIndex: "type",
          ellipsis: true,
          align: "center",
          width: "120px",
          scopedSlots: { customRender: "type" },
        },
        {
          title: "基本工资",
          dataIndex: "basicSalary",
          ellipsis: true,
          align: "right",
          width: "120px",
          scopedSlots: { customRender: "amount" },
        },
        {
          title: "回款提成",
          dataIndex: "huikuanticheng",
          align: "right",
          width: "120px",
          scopedSlots: { customRender: "amount" },
        },
        {
          title: "分润提成",
          dataIndex: "fenrunticheng",
          ellipsis: true,
          align: "right",
          width: "120px",
          scopedSlots: { customRender: "amount" },
        },
        {
          title: "签约笔数",
          dataIndex: "jinjainshu",
          align: "right",
          width: "100px",
        },
        {
          title: "客服签约提成",
          dataIndex: "jinjianticheng",
          ellipsis: true,
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "amount" },
        },
        {
          title: "协助客服签约提成",
          dataIndex: "halfjinjianticheng",
          ellipsis: true,
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "amount" },
        },
        {
          title: "回款金额",
          dataIndex: "checkHuikuan",
          ellipsis: true,
          align: "right",
          width: "150px",
          scopedSlots: { customRender: "amount" },
        },
        {
          title: "协助人数",
          dataIndex: "toxiezuCount",
          align: "right",
          width: "100px",
        },
        {
          title: "协助金额",
          dataIndex: "toxiezuAmount",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "amount" },
        },
        {
          title: "我的协助人数",
          dataIndex: "myxiezuCount",
          align: "right",
          width: "100px",
        },
        {
          title: "我的协助金额",
          dataIndex: "myxiezuAmount",
          align: "right",
          width: "130px",
          scopedSlots: { customRender: "amount" },
        },
      ],
    };
  },
  created() {
    this.getList();

    this.getDicts("gongzi_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDeptList();
  },
  methods: {
    getUserList(deptId) {
      listUser({ deptId: deptId, pageSize: 100 }).then((res) => {
        this.userNamesList = res.data.list;
      });
    },
    onMenuTreeChange(value) {
      this.getUserList(value);
    },
    getDeptList() {
      listDeptTree("", 3).then((response) => {
        // this.expandTree(response.data, 3, this.expandedRowKeys);
        this.deptOptions = response.data;
      });
    },
    handleChooseProduct(val, options) {
      // this.form.productId = val;
      // this.form.productName = options.key;
    },
    handleGetProductInfo(value) {
      listBizProductInfo({ orgType: value, pageSize: 50 }).then((res) => {
        this.productList = res.data.list;
      });
    },
    formatNumber(num) {
      return parseFloat(num || 0).toFixed(2);
    },
    /** 查询资方结算列表 */
    getList() {
      this.loading = true;
      yuangonggongzitongji(this.queryParam).then((response) => {
        this.bizCapBillList = response.data;
        // this.total = response.data.total;
        this.loading = false;
      });
    },
    // 结算类型字典翻译
    typeFormat(row) {
      if (row.type) {
        return this.selectDictLabel(this.typeOptions, row.type);
      } else {
        return "";
      }
    },
    // 结算类型字典翻译
    orgTypeFormat(row) {
      if (row.orgType) {
        return this.selectDictLabel(this.orgTypeOptions, row.orgType);
      } else {
        return "";
      }
    },
    // 结算状态字典翻译
    statusFormat(row) {
      if (row.status) {
        return this.selectDictLabel(this.statusOptions, row.status);
      } else {
        return "";
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        jinjianId: undefined,

        cusName: undefined,

        beginQdTime: undefined,

        status: undefined,
      };
      this.handleQuery();
    },
    /** 翻页操作 */
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSizeChange(current, size) {
      this.queryParam.pageNum = 1;
      this.queryParam.pageSize = size;
      this.getList();
    },
    /** 翻页操作 */
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current;
      this.queryParam.pageSize = pageSize;
      this.getList();
    },
    /** 翻页操作 */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.ids = this.selectedRows.map((item) => item.id);
      this.single = selectedRowKeys.length !== 1;
      this.multiple = !selectedRowKeys.length;
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    handleAdd(record) {
      if (record.status === "1") {
        this.$message.warning("请勿重复结算", 3);
        return;
      }
      this.showAddModal = true;
      this.$nextTick(() => this.$refs.bizCapBillAddForm.handleAdd(record));
    },
    handleUpdate(record, ids) {
      this.showEditModal = true;
      this.$nextTick(() =>
        this.$refs.bizCapBillEditForm.handleUpdate(record, ids)
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      var that = this;
      this.$confirm({
        title: "是否确认导出?",
        content: "此操作将导出当前条件下所有数据而非选中数据",
        onOk() {
          return exportOrgTypeStatistic(that.queryParam).then((response) => {
            that.download(response.msg);
            that.$message.success("导出成功", 3);
          });
        },
        onCancel() {},
      });
    },
  },
};
</script>
